<?php
/*******************************************************************************
 * Project: Bridge for WordPress to phpBB 3.0.x
 * File:    widget-user.php
 * Author:  The Greek WordPress Comunity
 * Date:    Tuesday, 1st February 2011
 * -----------------------------------------------------------------------------
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

class phpBB3Users extends WP_Widget
{
    function phpBB3Users()
    {
        /* Widget settings. */
        $widget_ops = array(
            'classname' => 'phpBB3 Users Widget',
            'description' => __('Allows you to display statistics and links about the user.', 'wp_phpbb_bridge')
        );
        
        /* Widget control settings. */
        $control_ops = array(
            'width' => 250,
            'height' => 250,
            'id_base' => 'phpbb3-users-widget'
        );
        
        /* Create the widget. */
        $this->WP_Widget(
            'phpbb3-users-widget', 
            'phpBB3 Users Widget', 
            $widget_ops, 
            $control_ops
        );
    }
    
    function form($instance)
    {
        $defaults = array(
                        'login_title'=>'Log In',
        				'info_title'=>'{USERNAME}',
        				'meta_links'=>'yes',
                    );
    ?>
    <div class="widget-content">
    	<p>
            <label for="<?php echo $this->get_field_id('login_title'); ?>">
                <?php 
                    echo _e('Login Box Title:', 'wp_phpbb_bridge'); 
                ?>
            </label> 
            <input id="<?php echo $this->get_field_id('login_title'); ?>" name="<?php echo $this->get_field_name('login_title'); ?>" type="text" value="<?php echo $instance['login_title']; ?>" />
        </p>
    	<p>
            <label for="<?php echo $this->get_field_id('info_title'); ?>">
                <?php 
                    echo _e('User Info Title:', 'wp_phpbb_bridge');
                ?>
            </label>
            <input id="<?php echo $this->get_field_id('info_title'); ?>" name="<?php echo $this->get_field_name('info_title'); ?>" type="text" value="<?php echo $instance['info_title']; ?>" />
            <br />
            <small><?php _e('You can use the tag {USERNAME} to get the username. ex: Control Panel for user {USERNAME}', 'wp_phpbb_bridge'); ?></small>
        </p>
    	<p> 
            <input name="<?php echo $this->get_field_name('meta_links'); ?>" type="checkbox" id="<?php echo $this->get_field_id('meta_links'); ?>" value="yes" <?php if ($instance['meta_links'] == "yes") { echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('meta_links'); ?>">
                <?php 
                    echo _e('Display Meta Links:', 'wp_phpbb_bridge');
                ?>
            </label>
        </p>
    </div>
    <?php
    }
    
    function update($new_instance, $old_instance)
    {
        $instance = $old_instance;
        
        $instance['login_title'] = $new_instance['login_title'];
        $instance['info_title'] = $new_instance['info_title'];
        $instance['meta_links'] = $new_instance['meta_links'];
        
        return $instance;
    }
    
    function widget($args, $instance)
    {
        global $user;
    	
        extract($args);
    
    	$options = get_option('widget_wpb_user');

    	$login_title = $instance['login_title'];
    	$info_title = $instance['info_title'];
    	$meta_links = $instance['meta_links'];

        $userLogin = wpb_is_user_logged_in();

    	$title = '';
    	if($userLogin)
    	{
    		$title = $info_title;
    	}
    	else
    	{
    		$title = $login_title;
    	}
    
        if(preg_match("/\{USERNAME\}/", $title, $res) == 1)
        {
            $title = preg_replace("/\{USERNAME\}/", trim($user->data['username']), $title);
        }
    
    	if(empty($title))
    	{
    		$title = "&nbsp;";
    	}
    
    	echo $before_widget . $before_title . $title . $after_title;
        
    	if($userLogin)
    	{
    		widget_wpb_user_info();
    	}
    	else
    	{
    		widget_wpb_user_login();
    	}
        
    	echo $after_widget;
    }
}

function registerUsersWidget()
{
    register_widget('phpBB3Users');
}

add_action('widgets_init', 'registerUsersWidget');

//---------------------------------------------------------------------------------------------------

function widget_wpb_user_info()
{
	global $user;
    
	$avatar = wpb_get_avatar();
	$ucp_url = trim(get_option('wpb_url'));
	$forum_url = str_replace("/ucp.php", "/", $ucp_url);
	$admin_url = wpb_get_admin_link();
	$options = get_option('widget_wpb_user');
	$meta_links = htmlspecialchars($options['meta_links'], ENT_QUOTES);
    
    /* Transfer info */
	$mcp_url = wpb_get_mcp_link();

        if(!empty($avatar))
        { 
    ?>
        <br />
        <a href="<?php echo $ucp_url; ?>" title="<?php echo _e('User Control Panel', 'wp_phpbb_bridge'); ?>">
            <img src="<?php echo $avatar; ?>" alt="<?php echo _e('Avatar for', 'wp_phpbb_bridge'); ?> <?php echo $user->data['username']; ?>" />
        </a>
        <br />
    <?php
        }
    ?>
    <ul>
        <li>
            <?php
                echo _e('Posts:', 'wp_phpbb_bridge');
            ?>
            <a href="<?php echo $forum_url; ?>search.php?search_id=egosearch">
                <?php
                    echo number_format($user->data['user_posts']);
                ?>
            </a>
        </li>
        <li>
            <?php
                echo _e('New PMs:', 'wp_phpbb_bridge');
            ?>
            <a href="<?php echo $ucp_url; ?>?i=pm&amp;folder=inbox">
                <?php
                    echo number_format($user->data['user_new_privmsg']);
                ?>
            </a>
        </li>
        <li>
            <?php
                echo _e('Unread PMs:', 'wp_phpbb_bridge');
            ?>
            <a href="<?php echo $ucp_url; ?>?i=pm&amp;folder=inbox">
                <?php
                    echo number_format($user->data['user_unread_privmsg']);
                ?>
            </a>
        </li>
        <?php
        
            if($meta_links == "yes")
            { 
        
                if(!empty($admin_url))
                { 
                ?>
            	<li>
                    <a href="<?php echo $admin_url; ?>">
                        <?php 
                            echo _e('Forum administration', 'wp_phpbb_bridge'); 
                        ?>
                    </a>
                </li>
        <?php 
                } 
         
            } 
        ?>
        <?php 
            if(wpb_is_user_logged_in())
            {
        ?>
            <li>
                <a href="<?php echo get_option('home'); ?>/wp-admin/">
                    <?php 
                        echo _e('Blog control panel', 'wp_phpbb_bridge'); 
                    ?>
                </a>
            </li>
        	<li>
                <a href="<?php echo $ucp_url; ?>">
                    <?php 
                        echo _e('User control panel', 'wp_phpbb_bridge'); 
                    ?>
                </a>
            </li>
            <?php 
                if(!empty($mcp_url))
                { 
            ?>
        	<li>
                <a href="<?php echo $mcp_url; ?>">
                    <?php 
                        echo _e('Moderator control panel', 'wp_phpbb_bridge'); 
                    ?>
                </a>
            </li>
            <?php 
                } 
             
                if(!empty($permission_url))
                { 
            ?>
            <li>
                <a href="<?php echo $permission_url; ?>">
                    <?php 
                        echo _e('Restore permissions', 'wp_phpbb_bridge'); 
                    ?>
                </a>
            </li>
            <?php
                } 
            ?>
            <li>
                <a href="<?php echo $ucp_url; ?>?mode=logout&sid=<?php echo wpb_get_sessionid(); ?>">
                    <?php 
                        echo _e('Log out', 'wp_phpbb_bridge'); 
                    ?>
                </a>
            </li>
        <?php
            }
            else
            {
        ?>
            <li>
                <a href="<?php echo $ucp_url; ?>?mode=sendpassword">
                    <?php 
                        echo _e('I forgot my password', 'wp_phpbb_bridge'); 
                    ?>
                </a>
            </li>
        	<li>
                <a href="<?php echo $ucp_url; ?>?mode=resend_act">
                    <?php 
                        echo _e('Resend activation email', 'wp_phpbb_bridge'); 
                    ?>
                </a>
            </li>
        	<li>
                <a href="<?php echo $ucp_url; ?>?mode=register">
                    <?php 
                        echo _e('Register new account', 'wp_phpbb_bridge'); 
                    ?>
                </a>
            </li>
        <?php
            }
        ?>
    </ul>
<?php
}

function widget_wpb_user_init()
{
	if(!function_exists('register_sidebar_widget') || !wpb_is_active())
	{
		return;
	}
}

function widget_wpb_user($args)
{
	// DELETE ME YOU DONT NEED ME ANYMORE
}

function widget_wpb_user_control()
{
	$options = get_option('widget_wpb_user');
	if (!is_array($options))
	{
		$options = array('login_title'=>'Log In', 'info_title'=>'{USERNAME}', 'meta_links'=>'yes');
		update_option('widget_wpb_user', $options);
	}

	if ($_POST['widget-wpb-user-submit'])
	{
		$options['login_title'] = strip_tags(stripslashes($_POST['widget-wpb-user-login-title']));
		$options['info_title'] = strip_tags(stripslashes($_POST['widget-wpb-user-info-title']));
		$options['meta_links'] = $_POST['widget-wpb-user-meta-links'];
		update_option('widget_wpb_user', $options);
	}

	$login_title = htmlspecialchars($options['login_title'], ENT_QUOTES);
	$info_title = htmlspecialchars($options['info_title'], ENT_QUOTES);
	$meta_links = htmlspecialchars($options['meta_links'], ENT_QUOTES);
?>
	<p style="text-align:right;">
        <label for="widget-wpb-user-login-title">
            <?php 
                echo _e('Login Box Title:', 'wp_phpbb_bridge'); 
            ?> 
            <input style="width: 200px;" id="widget-wpb-user-login-title" name="widget-wpb-user-login-title" type="text" value="<?php echo $login_title; ?>" />
        </label>
    </p>
	<p style="text-align:right;">
        <label for="widget-wpb-user-info-title">
            <?php 
                echo _e('User Info Title:', 'wp_phpbb_bridge');
            ?>
            <input style="width: 200px;" id="widget-wpb-user-info-title" name="widget-wpb-user-info-title" type="text" value="<?php echo $info_title; ?>" />
        </label>
    </p>
	<p style="text-align:right;">
        <label for="widget-wpb-user-meta-links">
            <?php 
                echo _e('Display Meta Links:', 'wp_phpbb_bridge');
            ?> 
            <input name="widget-wpb-user-meta-links" type="checkbox" id="widget-wpb-user-meta-links" value="yes" <?php if ($meta_links == "yes") { echo 'checked="checked" '; } ?> />
        </label>
    </p>
	<input type="hidden" id="widget-wpb-user-submit" name="widget-wpb-user-submit" value="1" />
<?php
}

//register_sidebar_widget(array('phpBB3 User', 'widgets'), 'widget_wpb_user');
//register_widget_control(array('phpBB3 User', 'widgets'), 'widget_wpb_user_control', 300, 100);

function widget_wpb_user_login()
{
	$ucp_url = trim(get_option('wpb_url'));
	$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
	$options = get_option('widget_wpb_user');
	$meta_links = htmlspecialchars($options['meta_links'], ENT_QUOTES);
?>
    <form action="<?php echo $ucp_url; ?>?mode=login" method="post" id="login">
        <label for="username">
            <?php 
                echo _e('Username:', 'wp_phpbb_bridge'); 
            ?>
        </label> 
        <input type="text" name="username" id="username" size="8" class="widget-wpb-user-username" style="width: 187px;" />
        <label for="password">
            <?php 
                echo _e('Password:', 'wp_phpbb_bridge'); 
            ?>
        </label> 
        <input type="password" id="password" name="password" size="8" class="widget-wpb-user-password" style="width: 187px;" />
        <label for="autologin" style="font-size: 85%;">
            <input type="checkbox" name="autologin" id="autologin" class="widget-wpb-user-autologin" /> 
            <?php 
                echo _e('Remember me', 'wp_phpbb_bridge'); 
            ?>
        </label>
        <label for="viewonline" style="font-size: 85%;">
            <input type="checkbox" name="viewonline" id="viewonline" class="widget-wpb-user-viewonline" /> 
            <?php 
                echo _e('Login as hidden', 'wp_phpbb_bridge'); 
            ?>
        </label>
        <input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
        <br />
    	<input type="submit" name="login" value="<?php echo _e('Login', 'wp_phpbb_bridge'); ?>" class="widget-wpb-user-login" />
        <?php 
            if($meta_links == "yes")
            { 
        ?>
            <br />
            <br />
            <a href="<?php echo $ucp_url; ?>?mode=sendpassword">
                <?php 
                    echo _e('I forgot my password', 'wp_phpbb_bridge'); 
                ?>
            </a>
            <br />
            <a href="<?php echo $ucp_url; ?>?mode=register">
                <?php 
                    echo _e('Register new account', 'wp_phpbb_bridge'); 
                ?>
            </a>
        <?php 
            } 
        ?>
    </form>
<?php
}

?>